package com.learn.sqlsession;

import com.learn.config.XmlConfigBuilder;
import com.learn.pojo.Configuration;
import com.learn.sqlsession.impl.DefaultSqlSessionFactory;
import org.dom4j.DocumentException;

import java.beans.PropertyVetoException;
import java.io.InputStream;

public class SqlSessionFactoryBuilder {
    private static SqlSessionFactoryBuilder sqlSessionFactoryBuilder;

    private SqlSessionFactoryBuilder() {
    }

    public static SqlSessionFactoryBuilder getInstance() {
        if (sqlSessionFactoryBuilder == null) {
            sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        }
        return sqlSessionFactoryBuilder;
    }

    public SqlSessionFactory build(InputStream input) throws DocumentException, PropertyVetoException {
        //1.读取配置文件信息 并且封装到Configuration中
        Configuration configuration = XmlConfigBuilder.parseConfig(input);
        //2.将封装的Configuration对象 当做 SqlSessionFactory 的构造函数参数 传入 并且成功创建 SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new DefaultSqlSessionFactory(configuration);
        return sqlSessionFactory;
    }
}
